package com.atguigu.bigdata.spark.core.rdd.operator.transform;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
import scala.Tuple2;

import java.util.ArrayList;
import java.util.List;

public class Spark12_RDD_Operator_Transform1_JAVA {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf().setMaster("local[*]").setAppName("sparkCore");
        JavaSparkContext sc = new JavaSparkContext(conf);
        // sortBy方法可以根据指定的规则对数据源中的数据进行排序，默认为升序，第二个参数可以改变排序的方式
        // sortBy默认情况下，不会改变分区。但是中间存在shuffle操作
        List<Tuple2<String, Integer>> list = new ArrayList<>();
        list.add(new Tuple2<String, Integer>("444",4));
        list.add(new Tuple2<String, Integer>("333",3));
        list.add(new Tuple2<String, Integer>("222",2));
        list.add(new Tuple2<String, Integer>("111",1));
        JavaRDD<Tuple2<String, Integer>> rdd = sc.parallelize(list, 2);
        //根据tuple的第一个元素排序
        JavaRDD<Tuple2<String, Integer>> sortby = rdd.sortBy(new Function<Tuple2<String, Integer>, String>() {
            @Override
            public String call(Tuple2<String, Integer> stringIntegerTuple2) throws Exception {
                return stringIntegerTuple2._1;
            }
        }, true, 2);

        System.out.println(sortby.collect().toString());

        sc.stop();
    }
}
